<?php
namespace app\controller\api;

use app\service\DeviceBaseService;
use app\validate\DeviceBaseValidate;
use share\controller\ApiBaseController;
use share\service\NotifyDingTalkService;
use think\Log;

class DeviceBaseController extends ApiBaseController{

    protected $beforeActionList = [
        // "checkSign",
        // "checkUser", 
    ];

    /**
     * @description: 
     * @return {*}
     * @use: 
     */
    public function initValidate(){
        $this->validate = new DeviceBaseValidate();
    }

    /**
     * 获取设备基础信息
     */
    public function get_simple_action(){ 
        $input = $this->check();
        $pk = $input['pk'];
        $dn = $input['dn'];
        $res = DeviceBaseService::getByDn($dn,[
            'pk' => $pk
        ]);
        if($res){
            $sn = $res['serial_no'];
            $iotId = $res['iot_id'];
            if(empty($iotId)){ // 没有iotId 无效
                return self::ok(null);
            }
            if(!empty($sn)){
                // 有相同序列号时 代表设备有问题  不返回数据
                $devices = DeviceBaseService::getAllByFilter([
                    'serial_no' => $sn,
                ]);
                if(count($devices) > 1){
                    trace("HasSameSerialNo PK:{$pk} DN:{$dn}. devices:".json_encode($devices),Log::ERROR);  

                    NotifyDingTalkService::pushTextByAMQP("设备序列号相同  PK:{$pk} DN:{$dn}. devices:".json_encode($devices));
                
                    //不返回数据
                    $res = null;
                }
            }
            $iotId = $res['iot_id'];
            if(empty($iotId)){

            }
        }
        return self::ok($res);
    }


}
